package com.springboot.admin.servlet;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/************************************************************************
 *
 *    Copyright  Copyright 2021 Google Inc.
 *    File Name: MyRegistConfig
 *    Description: 自定义servlet，filter，listener
 *                 1、MyServlet --> /my
 *                 2、DispatcherServlet --> /
 *
 *    Version: V1.0
 *    Author: Fuc
 *    Create Time: 2021-01-08
 *
 *************************************************************************/

// (proxyBeanMethods = true)：保证依赖的组件始终是单实例的
@Configuration(proxyBeanMethods = true)
public class MyRegistConfig {


  @Bean
  public ServletRegistrationBean myServlet() {
    MyServlet myServlet = new MyServlet();

    return new ServletRegistrationBean(myServlet, "/my", "/my02");
  }

  @Bean
  public FilterRegistrationBean myFilter() {

    MyFilter myFilter = new MyFilter();
    //        return new FilterRegistrationBean(myFilter,myServlet());
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
    filterRegistrationBean.setUrlPatterns(Arrays.asList("/my", "/css/*"));
    return filterRegistrationBean;
  }

  @Bean
  public ServletListenerRegistrationBean myListener() {
    MyServletContextListener myServletContextListener = new MyServletContextListener();
    return new ServletListenerRegistrationBean(myServletContextListener);
  }

}
